using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;

public class SpawnManager : MonoBehaviour
{
    [SerializeField]private GameObject[] spawnPrefabs;
    private float positionX = 5.3f;
    private float positionY = 2.5f;

    [SerializeField] private Transform Props;
    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("SpawnObjects", 3f, 10f);
    }

    void SpawnObjects()
    {
        foreach (GameObject obj in spawnPrefabs)
        {
            Instantiate(obj, RandomPosition(), Quaternion.identity,Props);
        }
    }

    int maxAttempts = 100;  // 最大尝试次数

    Vector3 RandomPosition()
    {
        for (int i = 0; i < maxAttempts; i++)
        {
            Vector3 randomPosition = new Vector3(Random.Range(-positionX, positionX), Random.Range(-positionY, positionY), 0);
            Collider[] colliders = Physics.OverlapSphere(randomPosition, 1f);
            if (colliders.Length == 0)
            {
                return randomPosition; 
            }
        }
        return Vector3.zero;  // 或者其他默认位置
    }
}
